﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Controller;

namespace IAI
{
    //This class was made by Tien and edited by Mads Enemark
    public partial class Login : Window
    {
        #region Private
        private Settings1 _settings;
        private Grid _grid;
        private GroupBox _gbLogin;
        private MyController _myController;
        private UCLive _ucLive;
        #endregion

        public Login()
        {
            _settings = new Settings1();
            _myController = new MyController(_settings.DataSource, _settings.Username, _settings.Password);
            if (_myController.IsConnected) //Check db connection through controller
            {
                InitializeComponent();
                _grid = gridContent;
                _gbLogin = gbLogin;
                txtInitials.Focus(); //Tien - 27. maj - Put txtInitials to default

            }
            else
            {
                MessageBox.Show("Der er ikke forbindelse til databasen. " +
                                    "\nProgrammet kan ikke køre og afslutter når denne besked er lukket." +
                                    " \n Prøv venligst at oprette forbindelse til databasen og prøv igen.",
                                    "Ingen forbindelse til databasen.");
                this.Close();

            }

            //_myController = new MyController();
            //if (!_myController.IsConnected) //Check db connection through controller
            //{
            //    MessageBox.Show("Der er ikke forbindelse til databasen. " +
            //                    "\nProgrammet kan ikke køre og afslutter når denne besked er lukket." +
            //                    " \n Prøv venligst at oprette forbindelse til databasen og prøv igen.",
            //                    "Ingen forbindelse til databasen.");
            //    this.Close();
            //}
            //else
            //{
            //    _grid = gridContent;
            //    _gbLogin = gbLogin;
            //    txtInitials.Focus(); //Tien - 27. maj - Put txtInitials to default
            //}
        }

        #region Methods
        public void CheckLogin()
        {
            string initials = txtInitials.Text, password = passPassword.Password;

            foreach (var o in _myController.GetOperator())
            {
                if (o.Initials == initials && o.Password == password && o.IsEnabled == true)
                {
                    txtInitials.Text = string.Empty;
                    passPassword.Password = string.Empty;
                    _myController.CurrentOperator = o;
                    _myController.AddOperatorSession2DB(o.Initials, DateTime.Now, DateTime.Now);
                    return;
                }
            }
        }
        public bool ValidateInput(Control control, string message)
        {
            string str = string.Empty;
            if (control is TextBox)
            {
                TextBox txt = (TextBox)control;
                str = txt.Text;
            }
            if (control is PasswordBox)
            {
                PasswordBox pass = (PasswordBox)control;
                str = pass.Password;
            }
            if (str.Length == 0)
            {
                MessageBox.Show(message);
                control.Focus();
                return false;
            }
            return true;
        }
        public bool ValidateLoginInput()
        {
            if (ValidateInput(txtInitials, "Du mangler at indtaste signatur") == false)
                return false;
            if (ValidateInput(passPassword, "Du mangler at indtaste kode") == false)
                return false;
            return true;
        }
        public void ShowUserControl(UIElement uiElement, string title, double windowHeight = 650, double windowWidth = 900)
        {
            menuGo2.IsEnabled = true;
            menuLogout.IsEnabled = true;
            _grid.Children.Clear();
            _grid.Children.Add(uiElement);
            _grid.Children[0].Focus();
            FocusManager.GetFocusedElement(_grid);
            this.Height = windowHeight;
            this.Width = windowWidth;
            this.Title = title;
            dockPanel1.Width = _grid.Width;
            dockPanel1.HorizontalAlignment = HorizontalAlignment.Stretch;
            menu1.Width = _grid.Width;
            menu1.HorizontalAlignment = HorizontalAlignment.Stretch;
            menuSettings.Visibility = Visibility.Collapsed;
            menuEdit.Visibility = Visibility.Collapsed;
            menuHelpGo2Hotkeys.Visibility = Visibility.Collapsed;
        }
        public void ShowUCLive()
        {
            //Set UCLive
            _ucLive = new UCLive(_myController);
            ShowUserControl(_ucLive, "Live visning");
            menuSettings.Visibility = Visibility.Visible;
            menuEdit.Visibility = Visibility.Visible;
            menuEdit.IsEnabled = true;
            menuHelpGo2Hotkeys.Visibility = Visibility.Visible;
            if (_myController.CurrentOperator.Initials == "000") //is admin, then show go2 menu
            {
                menuGo2.IsEnabled = true;
                menuGo2.Visibility = Visibility.Visible;
            }
        }
        public void ShowUCOperator()
        {
            ShowUserControl(new UCOperator(_myController), "Operatør oversigt");
        }
        public void UpdateUserControl(UIElement uiElement)
        {
            _grid.Children[0].Focus();
            FocusManager.GetFocusedElement(_grid);
        }
        public void CloseWindow()
        {
            if (_myController.CurrentOperator != null)
                _myController.UpdateOperatorSession(DateTime.Now);
            this.Close();
        }
        #endregion

        #region Events
        private void btnLive_Click(object sender, RoutedEventArgs e)
        {
            if (ValidateLoginInput() == false)
                return;
            CheckLogin();
            if (_myController.CurrentOperator != null)
                ShowUCLive();
            else
            {
                MessageBox.Show("Du har indtastet forkert signatur eller kode.");
                txtInitials.Focus();
            }
        }
        private void Grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Grid g = (Grid)sender;
            dockPanel1.Width = g.Width;
            menu1.Width = g.Width;
        }
        private void MenuLogout_Click(object sender, RoutedEventArgs e)
        {
            _myController.CurrentOperator = null; //remove current operator
            _myController.UpdateOperatorSession(DateTime.Now);
            ShowUserControl(_gbLogin, "Login", 350, 550);
            menuGo2.IsEnabled = false;
            menuLogout.IsEnabled = false;
            menuEdit.IsEnabled = false;
            menuGo2.Visibility = Visibility.Collapsed;
            _ucLive = null;
        }
        private void MenuClose_Click(object sender, RoutedEventArgs e)
        {
            CloseWindow();
        }
        private void MenuEditFeatureList_OnClick(object sender, RoutedEventArgs e)
        {
            _ucLive.tiList.IsSelected = true;
            UpdateUserControl(_ucLive);
        }
        private void MenuEditChart_OnClick(object sender, RoutedEventArgs e)
        {
            _ucLive.tiGraph.IsSelected = true;
            _ucLive.tiGraph.IsEnabled = true;
            _ucLive.dgFeature.SelectedItem = _myController.CurrentFeatureMask;
            _ucLive.ShowChart(_myController.CurrentFeatureMask);
            UpdateUserControl(_ucLive);
        }
        private void MenuEditFind_OnClick(object sender, RoutedEventArgs e)
        {
            _ucLive.txtSearch.Focus();
            FocusManager.GetFocusedElement(_ucLive.txtSearch);
        }
        private void MenuEditRefresh_OnClick(object sender, RoutedEventArgs e)
        {
            _ucLive.btnUpdateData.RaiseEvent(new RoutedEventArgs(Button.ClickEvent));
        }
        private void MenuGo2Live_Click(object sender, RoutedEventArgs e)
        {
            ShowUCLive();
        }
        private void MenuGo2Operator_Click(object sender, RoutedEventArgs e)
        {
            ShowUCOperator();
        }
        private void MenuSettings_Click(object sender, RoutedEventArgs e)
        {
            Settings s = new Settings(_myController);
            s.Owner = this;
            s.ShowDialog();
        }
        private void MenuHelp_Click(object sender, RoutedEventArgs e)
        {
            Help h = new Help();
            h.Show();
        }
        private void MenuHelpGo2Hotkeys_OnClick(object sender, RoutedEventArgs e)
        {
            Keys k = new Keys();
            k.ShowDialog();
        }
        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (Keyboard.IsKeyDown(Key.LeftCtrl) && Keyboard.IsKeyDown(Key.W))
                CloseWindow();
        }
        private void Window_Closed(object sender, EventArgs e)
        {
            _myController.UpdateOperatorSession(DateTime.Now);
        }
        #endregion
    }
}
